package com.sanda.androidcourse.chapter07;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.sanda.androidcourse.R;

public class MyProviderActivity extends AppCompatActivity {

    TextView txtStudentContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my_provider);

        txtStudentContent = findViewById(R.id.txt_student_content);
        readStudents();
    }

    private void readStudents() {
        StringBuilder studentsData = new StringBuilder();

        Cursor cursor = getContentResolver().query(Uri.withAppendedPath(StudentProvider.CONTENT_URI, "2"), null, null, null, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
                String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                int age = cursor.getInt(cursor.getColumnIndexOrThrow("age"));
                studentsData.append(id).append(": ").append(name).append(", Age: ").append(age).append("\n");
            }
            cursor.close();
        }
        else {
            studentsData.append("No students found.");
        }
        txtStudentContent.setText(studentsData.toString());
    }
}